#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int gNum = 100;

void test()
{
    int num = 10;
    int age = 33;
    string name("lili");
    [num, &age, name](int value){
        ++value;
        cout << "value = " << value << endl;
        cout << "num = " << num << endl;
        ++age;
        cout << "age = " << age << endl;
        cout << "name = " << name << endl;
        ++gNum;
        cout << "gNum = " << gNum << endl;
    }(100);

    cout << endl << endl;
    //age是引用捕获，其他所有外部变量都是值捕获=
    [=, &age](int value){
        ++value;
        cout << "value = " << value << endl;
        cout << "num = " << num << endl;
        ++age;
        cout << "age = " << age << endl;
        cout << "name = " << name << endl;
    }(100);

    cout << endl << endl;
    //name是值捕获，其他所有外部变量都是引用捕获&
    [&, name](int value){
        ++value;
        cout << "value = " << value << endl;
        ++num;
        cout << "num = " << num << endl;
        ++age;
        cout << "age = " << age << endl;
        cout << "name = " << name << endl;
    }(100);

    cout << endl << endl;
    //所有外部变量都是值捕获=
    [=](int value){
        ++value;
        cout << "value = " << value << endl;
        cout << "num = " << num << endl;
        cout << "age = " << age << endl;
        cout << "name = " << name << endl;
    }(100);

    cout << endl << endl;
    //所有外部变量都是引用捕获&
    [&](int value){
        ++value;
        cout << "value = " << value << endl;
        ++num;
        cout << "num = " << num << endl;
        ++age;
        cout << "age = " << age << endl;
        name = "wangdao";
        cout << "name = " << name << endl;
    }(100);
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

